<?php

namespace App\Http\Controllers;

use App\Http\Model\Feedback;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Http\Service\ConfigService;
use App\Http\Service\VersionService;
use App\Http\Service\CaptchaService;
use App\Http\Service\TokenService;
use Illuminate\Http\Request;
use Gregwar\Captcha\CaptchaBuilder;
use App\Libs\Dysms\Sms;

class AppController extends BaseController
{

    protected $request;
    protected $user;
    protected $configService;
    protected $versionService;
    protected $captchaService;
    protected $tokenService;

    public function __construct(Request $request,
                                ConfigService $configService,
                                VersionService $versionService,
                                CaptchaService $captchaService,
                                TokenService $tokenService)
    {
        $this->request = $request;
        $this->user = $this->request->get('user');
        $this->configService = $configService;
        $this->versionService = $versionService;
        $this->captchaService = $captchaService;
        $this->tokenService = $tokenService;
    }

    public function config()
    {
        $config = [];

        // app配置
        $appConfig = $this->configService->findFirstRowByFields(['type' => 'app_config']);

        $config += $appConfig['content'];

        // 版本信息
        $systemType = $this->request->header('system-type');
        $systemVersion = $this->request->header('system-version');

        // 用户信息
        $isCreateToken = $this->request->get('is_create_token');

        if ($this->user->id != 0) {
            $this->user->system_type = $systemType;
            $this->user->system_version = $systemVersion;
            $this->user->updated_at = date('Y-m-d H:i:s');
            $this->user->save();
        }

        // 给未登录用户分配token
        if ($isCreateToken) {
            $jwt = encodeToken($this->user);
            $token = $this->tokenService->create(['token' => $jwt, 'token_crc32' => crc32($jwt)]);
            $this->user->token = $token->token;
        }

        $config['user'] = $this->user;
        $config['system_type'] = $systemType;
        $config['version'] = $this->versionService->findFirstRowByFields(['system_type' => $systemType]);

        // 七牛token
        $config['qiniu_upload_token'] = getQiniuUploadToken();

        $config['cdn_domain'] = env('CDN_DOMAIN');

        return toJSON($config);
    }

    public function captcha()
    {
        $widthDefault = 100;
        $heightDefault = 40;
        $width = $this->request->input('width', $widthDefault);
        $height = $this->request->input('height', $heightDefault);

        $token = $this->request->input('token');


        if ($width > 400 || $heightDefault > 40) {
            $width = $widthDefault;
            $height = $heightDefault;
        }
        
        $code = rand(1000, 9999);
        $builder = new CaptchaBuilder($code);
        $builder->setDistortion(true); // 验证码是否变形
        $builder->setInterpolation(false);
        //$builder->setIgnoreAllEffects(true); // 屏蔽所有干扰项
        $builder->setMaxBehindLines(5); // 设置验证码后面几根线
        //$builder->setMaxFrontLines(0); // 设置验证码前面几根线

        $builder->build($width, $height);
        //$code = $builder->getPhrase();

        $this->captchaService->setImageCaptcha($code, $this->user, $token);

        header('Content-type: image/jpeg');
        $builder->output();
    }

    public function sms() // Short Message Service 手机短信服务
    {
        $mobile = $this->request->input('mobile');
        $imageCaptcha = $this->request->input('captcha');
        $token = $this->request->header('token');

        // 手机号验证
        if (!checkMobile($mobile))
            return toJSON('', \Constant::MOBILE_ERROR);

        // 图形验证码验证
        $checkImageCaptcha = $this->captchaService->checkImageCaptcha($imageCaptcha, $this->user, $token);
        if (!$checkImageCaptcha)
            return toJSON('', \Constant::IMAGE_CAPTCHA_ERROR);

        $code = rand(1000, 9999);

        $sendSuccess = false;
        try {
            $sms = new Sms();
            $sendSuccess = $sms->send($mobile, $code);

        } catch (\Exception $e) {
            // todo 报警 记日志
        }

        if (!$sendSuccess) {
            return toJSON('', \Constant::FAILED);
        }

        $this->captchaService->setMobileCaptcha($code, $this->user, $token, $mobile);

        return toJSON();
    }

    public function feedback()
    {

        $content = $this->request->input('content', '');
        $contact = $this->request->input('contact', '');

        if (strlen($content) == 0) {
            return toJSON('', \Constant::NEED_SHOW_MESSAGE, '反馈内容不能为空');
        }

        $feedback = new Feedback;
        $feedback->user_id = $this->user->id;
        $feedback->content = $content;
        $feedback->contact = $contact;
        $feedback->save();

        return toJSON();

    }

    public function info()
    {
        $pwd = $this->request->input('pwd');

        if ($pwd != 'lmm') return;

        phpinfo();
    }
}
